package com.example.wsdemo.axis;

import org.apache.axis.AxisProperties;
import org.apache.axis.ConfigurationException;
import org.apache.axis.EngineConfiguration;
import org.apache.axis.configuration.EngineConfigurationFactoryDefault;
import org.apache.axis.configuration.FileProvider;
import org.apache.axis.server.AxisServer;
import org.apache.axis.utils.ClassUtils;
import org.apache.axis.utils.Messages;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import java.io.File;
import java.io.InputStream;

/**
 * @author chenc
 */
public class EngineConfigurationFactory extends EngineConfigurationFactoryDefault {

    private ServletConfig cfg;

    public static org.apache.axis.EngineConfigurationFactory newFactory(Object param) {
        return (param instanceof ServletConfig)
                ? new EngineConfigurationFactory((ServletConfig) param)
                : null;
    }

    protected EngineConfigurationFactory(ServletConfig cfg) {
        super();
        this.cfg = cfg;
    }

    @Override
    public EngineConfiguration getServerEngineConfig() {
        ServletContext ctx = cfg.getServletContext();

        String configFile = cfg.getInitParameter(OPTION_SERVER_CONFIG_FILE);
        if (configFile == null) {
            configFile =
                    AxisProperties.getProperty(OPTION_SERVER_CONFIG_FILE);
        }
        if (configFile == null) {
            configFile = SERVER_CONFIG_FILE;
        }

        String appWebInfPath = "/WEB-INF";

        FileProvider config = null;

        String realWebInfPath = ctx.getRealPath(appWebInfPath);

        if (realWebInfPath == null ||
                !(new File(realWebInfPath, configFile)).exists()) {

            String name = appWebInfPath + "/" + configFile;
            InputStream is = ctx.getResourceAsStream(name);
            if (is != null) {
                config = new FileProvider(is);
            }

            name = configFile;
            ClassLoader cl = Thread.currentThread().getContextClassLoader();
            is = cl.getResourceAsStream(name);
            if (is != null) {
                config = new FileProvider(is);
            }

            if (config == null) {
                log.error(Messages.getMessage("servletEngineWebInfError03",
                        name));
            }
        }

        if (config == null && realWebInfPath != null) {
            try {
                config = new FileProvider(realWebInfPath, configFile);
            } catch (ConfigurationException e) {
                log.error(Messages.getMessage("servletEngineWebInfError00"), e);
            }
        }

        if (config == null) {
            log.warn(Messages.getMessage("servletEngineWebInfWarn00"));
            try {
                InputStream is =
                        ClassUtils.getResourceAsStream(AxisServer.class,
                                SERVER_CONFIG_FILE);
                config = new FileProvider(is);
            } catch (Exception e) {
                log.error(Messages.getMessage("servletEngineWebInfError02"), e);
            }
        }
        return config;
    }
}
